package com.joinway.framework.utils.json;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

@Deprecated
public final class JsonConverter {

	@SuppressWarnings("unchecked")
	public static <T> T jsonToObject(String text, TypeReference<T> ref) throws Exception {
		ObjectMapper mapper = createMapper();
		
		return (T) mapper.readValue(text.getBytes(), ref);
	}

	public static String objectToJson(Object obj) throws Exception {
		return objectToJson(obj, true);
	}

	public static String objectToJson(Object obj, boolean prettyPrint) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		return prettyPrint ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj) : mapper.writeValueAsString(obj);
	}
	
	private static ObjectMapper createMapper() {
		return new ObjectMapper()
				.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,true)
				.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}
}
